package io.polly.thread.util;

import io.polly.common.constant.NotifyTypeEnum;
import io.polly.common.entity.NotifyItem;
import io.polly.thread.DynamicExecutor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;

/**
 * @author ldz
 * @version V1.0
 * @title: NotifyHelper
 * @description: TODO(用一句话描述该文件做什么)
 * @date: 2022/3/22 16:06
 */
@Slf4j
public class NotifyHelper {


    public static NotifyItem getNotifyItem(DynamicExecutor dynamicExecutor, NotifyTypeEnum typeEnum) {
        List<NotifyItem> notifyItems = dynamicExecutor.getNotifyItems();
        NotifyItem notifyItem = notifyItems.stream()
                .filter(item -> typeEnum.getValue().equalsIgnoreCase(item.getType().getValue()) && item.isEnabled())
                .findFirst()
                .orElse(null);
        if (Objects.isNull(notifyItem)) {
            log.warn("DynamicExecutor notify, no such [{}] notify item configured, threadPoolName: {}",
                    typeEnum.getValue(), dynamicExecutor.getThreadPoolName());
            return null;
        }

        return notifyItem;
    }

}
